package cn.edu.swpu.sims.config;

import cn.edu.swpu.sims.model.Admin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 码之泪殇
 * @date 2020/9/11 23:04
 * 到达胜利之前，无法回头
 */
@Slf4j
public class MyInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null || "".equals(admin.getUsername())) {
            request.setAttribute("msg","您还没有登录,请先登录");
            request.setAttribute("uri","index.jsp");
            request.getRequestDispatcher(request.getContextPath()+"/401.jsp").forward(request,response);
            return false;
        }
        return true;
    }
}
